<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="班级代码">
        <el-input v-model="classs.classNo" maxlength="10"/>
      </el-form-item>
      <el-form-item label="班级名称">
        <el-input v-model="classs.className"/>
      </el-form-item>
      <!-- 所属院系 -->
      <el-form-item label="所属学院">
        <el-select v-model="classs.deptId" filterable placeholder="请选择" @change="departmentChanged">
          <el-option
            v-for="dept in departments"
            :key="dept.id"
            :label="dept.deptName"
            :value="dept.id"/>
        </el-select>
      </el-form-item>
      <!-- 所属专业 -->
      <el-form-item label="所属专业">
        <el-select v-model="classs.majorId" filterable placeholder="请选择">
          <el-option
            v-for="major in majors"
            :key="major.id"
            :label="major.majorName"
            :value="major.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="班辅导员">
        <el-input v-model="classs.classInstructor"/>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          v-model="classs.classRemark"
          type="textarea"
          placeholder="请输入内容"
          maxlength="30"
          show-word-limit/>
      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style lang="scss" scoped>
  .app-container{
    width: 500px;
  }
</style>

<script>
import major from '@/api/student/major'
import department from '@/api/student/department'
import classs from '@/api/student/classs'

const defaultForm = {
  classNo: '',
  className: '',
  deptId: null,
  majorId: null,
  classInstructor: '',
  classRemark: ''
}

export default {
  data() {
    return {
      departments: [],
      majors: [],
      classs: defaultForm,
      saveBtnDisabled: false, // 保存按钮是否禁用,
      BASE_API: process.env.BASE_API // 接口API地址
    }
  },

  watch: {
    $route(to, from) {
      console.log('watch $route')
      this.init()
    }
  },

  created() {
    console.log('created .....')
    this.init()
  },

  methods: {
    init() {
      console.log('init .....')
      if (this.$route.params && this.$route.params.id) { // 更新
        const id = this.$route.params.id
        this.fetchDataById(id)
      } else { // 新增
        // 使用对象拓展运算符，拷贝对象，而不是引用
        this.classs = { ...defaultForm }
      }

      // 获取院系列表
      department.getList().then(response => {
        // debugger 设置断点调试
        if (response.success === true) {
          this.departments = response.data.items
        }
      })
    },
    departmentChanged(value) {
      console.log(value)
      // 获取专业列表
      major.getList(value).then(response => {
        // debugger 设置断点调试
        if (response.success === true) {
          this.majors = response.data.items
        }
      })
    },
    // 保存
    saveOrUpdate() {
      this.saveBtnDisabled = true
      classs.save(this.classs).then(response => {
        console.log(response)
        return this.$message({
          type: 'success',
          message: response.message
        })
      }).then(resposne => {
        this.$router.push({ path: '/class' })
      })
    },

    // 根据id查询记录
    fetchDataById(id) {
      classs.getById(id).then(response => {
        this.classs = response.data.item
      })
    }

  }
}
</script>
